<?php

namespace App\Model\Service;


use App\Model\AuthMenuRuleModel;
use App\Model\AuthRoleModel;
use App\Model\BaseModel;
use Trois\Lib\Help;

class UserRulesService extends BaseService
{
    protected bool $isAdmin = false;

    protected array $group = [];
    protected array $rules = [];
    protected array $ruleTree = [];

    public function getIsAdmin(): bool
    {
        return $this->isAdmin;
    }

    public function getRules(): array
    {
        return $this->rules;
    }

    public function getRuleTree(): array
    {
        return $this->ruleTree;
    }

    public function getGroup(): array
    {
        return $this->group;
    }


    public function handle(int $userGroupId): bool
    {

        $where = [
            'ro_id' => $userGroupId,
            'status' => 1
        ];

        //角色（权限组）
        $authGroup = Help::modelInvoke(AuthRoleModel::class, function (BaseModel $model) use ($where) {
            return $model
                ->field('ro_id,ro_name,permission,permission_app,permission_erp,permission_cat,permission_oa,permission_shop,permission_console
                ,permission_boss,status,is_super,is_teacher')
                ->where($where)
                ->get();
        });

        if (!$authGroup) {
            $this->setError('无权限登陆，请联系上级领导分配权限组');
            return false;
        }

        $this->group = $authGroup;

//        dd($authGroup);

        $authGroupPermission = '';
        $authMenuRuleType = 9999;

        if ($this->isComeCrmPc()) {
            $authGroupPermission = $authGroup['permission'];
            $authMenuRuleType = 1; //crm
        }

        if (!$authGroupPermission) {
            $this->setError('权限组没有分配具体权限，请联系上级领导处理');
            return false;
        }

        if ($authMenuRuleType === 9999) {
            $this->setError('登陆端口异常');
            return false;
        }

        if ($authGroupPermission === '*') {
            //超级管理员
            $this->isAdmin = true;
            $where = [
                'menu_rule.type' => $authMenuRuleType,
                'menu.type' => $authMenuRuleType,
                'menu.status' => 0,
                'rule.status' => 1
            ];

        } else {
            $where = [
                'menu_rule.rule_id' => [explode(',', $authGroupPermission), 'in'],
                'menu_rule.type' => $authMenuRuleType,
                'menu.type' => $authMenuRuleType,
                'menu.status' => 0,
                'rule.status' => 1
            ];
        }

        $authRules = Help::modelInvoke(AuthMenuRuleModel::class, function (BaseModel $model) use ($where) {
            return $model
                ->field('menu_rule.menu_id,menu_rule.rule_id
                ,menu.parent_id as parent_id,menu.menu_name,menu.n_menu_url as menu_url,menu.n_menu_meta as menu_meta,menu.sort as menu_sort,menu.n_icon_type as icon_type,menu.n_icon as icon
                ,rule.parent_id as rule_parent_id,rule.rule_name,rule.rule_sign
                ')
                ->alias('menu_rule')
                ->join('by_crm_biz_menu_new as menu', 'menu_rule.menu_id = menu.menu_id', 'LEFT')
                ->join('by_crm_biz_rule_new as rule', 'menu_rule.rule_id = rule.rule_id', 'LEFT')
                ->where($where)
                ->order('menu.sort','asc')
                ->all();
        });

        if (!$authRules) {
            return true;
        }

        $this->rules = $authRules;

        $this->ruleTree = getTree($authRules
            , 0
            , 'parent_id'
            , 'menu_id'
            , 0
            , 'menu_name'
            , 'children'
            , []
            , ['menu_id'
                , 'menu_name'
                , 'parent_id'
                , 'menu_url'
                , 'icon'
                , 'icon_type'
                , 'menu_meta'
            ]);

        return true;
    }


}